package com.example.managemyself.data.model;

import androidx.room.Entity;
import androidx.room.Ignore;
import androidx.room.PrimaryKey;
import androidx.room.TypeConverters;
import androidx.room.ColumnInfo;

import com.example.managemyself.util.DateConverter;

import java.util.Date;
import java.util.List;
import java.util.ArrayList;

/**
 * 日记条目实体类
 */
@Entity(tableName = "diary_entries")
@TypeConverters({DateConverter.class})
public class DiaryEntry {
    @PrimaryKey(autoGenerate = true)
    private int id;
    
    private int userId;       // 用户ID
    private String title;
    private String content;
    private Date date;
    private String mood;
    private String weather;
    private String location;
    private String imageUri;  // 存储图片路径（保持向后兼容）
    
    // 新增多媒体附件支持
    private List<String> attachmentPaths;  // 附件路径列表
    private List<String> attachmentTypes;  // 附件类型列表（image, video, audio, document）
    
    // 已完成任务记录
    private List<String> completedTaskIds; // 当天完成的任务ID列表
    private String completedTasksSummary;  // 完成任务的文本摘要
    
    private Date createdDate; // 创建时间
    private Date updatedDate; // 更新时间
    
    // 默认构造函数（Room使用）
    public DiaryEntry() {
        this.createdDate = new Date();
        this.updatedDate = new Date();
        this.attachmentPaths = new ArrayList<>();
        this.attachmentTypes = new ArrayList<>();
        this.completedTaskIds = new ArrayList<>();
    }
    
    // 构造函数（应用程序使用，Room忽略）
    @Ignore
    public DiaryEntry(String title, String content, Date date) {
        this();
        this.title = title;
        this.content = content;
        this.date = date;
    }
    
    // 带用户ID的构造函数（应用程序使用，Room忽略）
    @Ignore
    public DiaryEntry(int userId, String dateStr, String content) {
        this();
        this.userId = userId;
        this.content = content;
        this.title = ""; // 默认标题为空
        
        // 解析日期字符串
        try {
            java.text.SimpleDateFormat format = new java.text.SimpleDateFormat("yyyy-MM-dd", java.util.Locale.getDefault());
            this.date = format.parse(dateStr);
        } catch (java.text.ParseException e) {
            this.date = new Date(); // 解析失败时使用当前日期
        }
    }
    
    // 附件相关方法
    public void addAttachment(String path, String type) {
        if (attachmentPaths == null) attachmentPaths = new ArrayList<>();
        if (attachmentTypes == null) attachmentTypes = new ArrayList<>();
        
        attachmentPaths.add(path);
        attachmentTypes.add(type);
        this.updatedDate = new Date();
    }
    
    public void removeAttachment(int index) {
        if (attachmentPaths != null && index >= 0 && index < attachmentPaths.size()) {
            attachmentPaths.remove(index);
            if (attachmentTypes != null && index < attachmentTypes.size()) {
                attachmentTypes.remove(index);
            }
            this.updatedDate = new Date();
        }
    }
    
    // 完成任务相关方法
    public void addCompletedTask(String taskId) {
        if (completedTaskIds == null) completedTaskIds = new ArrayList<>();
        if (!completedTaskIds.contains(taskId)) {
            completedTaskIds.add(taskId);
            this.updatedDate = new Date();
        }
    }
    
    public void updateCompletedTasksSummary(String summary) {
        this.completedTasksSummary = summary;
        this.updatedDate = new Date();
    }
    
    // Getters and Setters
    public int getId() {
        return id;
    }
    
    public void setId(int id) {
        this.id = id;
    }
    
    public int getUserId() {
        return userId;
    }
    
    public void setUserId(int userId) {
        this.userId = userId;
    }
    
    public String getTitle() {
        return title;
    }
    
    public void setTitle(String title) {
        this.title = title;
    }
    
    public String getContent() {
        return content;
    }
    
    public void setContent(String content) {
        this.content = content;
        this.updatedDate = new Date(); // 更新时间
    }
    
    public Date getDate() {
        return date;
    }
    
    public void setDate(Date date) {
        this.date = date;
    }
    
    public String getMood() {
        return mood;
    }
    
    public void setMood(String mood) {
        this.mood = mood;
    }
    
    public String getWeather() {
        return weather;
    }
    
    public void setWeather(String weather) {
        this.weather = weather;
    }
    
    public String getLocation() {
        return location;
    }
    
    public void setLocation(String location) {
        this.location = location;
    }
    
    public String getImageUri() {
        return imageUri;
    }
    
    public void setImageUri(String imageUri) {
        this.imageUri = imageUri;
    }
    
    // 新增字段的getter和setter
    public List<String> getAttachmentPaths() {
        return attachmentPaths;
    }
    
    public void setAttachmentPaths(List<String> attachmentPaths) {
        this.attachmentPaths = attachmentPaths;
    }
    
    public List<String> getAttachmentTypes() {
        return attachmentTypes;
    }
    
    public void setAttachmentTypes(List<String> attachmentTypes) {
        this.attachmentTypes = attachmentTypes;
    }
    
    public List<String> getCompletedTaskIds() {
        return completedTaskIds;
    }
    
    public void setCompletedTaskIds(List<String> completedTaskIds) {
        this.completedTaskIds = completedTaskIds;
    }
    
    public String getCompletedTasksSummary() {
        return completedTasksSummary;
    }
    
    public void setCompletedTasksSummary(String completedTasksSummary) {
        this.completedTasksSummary = completedTasksSummary;
    }
    
    public Date getCreatedDate() {
        return createdDate;
    }
    
    public void setCreatedDate(Date createdDate) {
        this.createdDate = createdDate;
    }
    
    public Date getUpdatedDate() {
        return updatedDate;
    }
    
    public void setUpdatedDate(Date updatedDate) {
        this.updatedDate = updatedDate;
    }
    
    // 别名方法
    public Date getCreatedAt() {
        return this.createdDate;
    }
    
    public void setCreatedAt(Date createdAt) {
        this.createdDate = createdAt;
    }
    
    public Date getUpdatedAt() {
        return this.updatedDate;
    }
    
    public void setUpdatedAt(Date updatedAt) {
        this.updatedDate = updatedAt;
    }
}
